<?php
class UserAccessRights extends Zend_Db_Table_Abstract {
	const ID = "id";
	const USER = "user_id";
	const ACCESS_RIGHT = "access_right";
	
	protected $_primary = "id";
	protected $_name = "user_access_rights";
	
	protected $_referenceMap = array (
    'User' => array (
        'columns' => 'user_id', 
        'refTableClass' => 'User', 
        'refColumns' => 'id'
    ),
    'AccessRight' => array (
    	'columns' => 'access_right',
    	'refTableClass' => 'AccessRight',
    	'refColumns' => 'id'
    )
    );
    
    /**
     * Add user access right.
     * 
     * @param $data
     */
    public function addEntry($data) {
    	$userAccessRight = $this->findByUnique($data[self::USER], $data[self::ACCESS_RIGHT]);
    	if (empty($userAccessRight)) {
    		$this->insert($data);
    	}
    }
    
    /**
     * Find by unique constraint.
     * 
     * @param $userId
     * @param $accessRight
     */
    public function findByUnique($userId, $accessRight) {
    	$select = $this->select()->where("user_id=?", $userId)->where("access_right=?", $accessRight);
    	return $this->fetchRow($select);
    }
    
	/**
     * Return access right of give user access right.
     * 
     * @param $userAccessRight
     */
    public static function getAccessRight($userAccessRight) {
        return $userAccessRight->findParentRow('AccessRight', 'AccessRight');
    }
}
?>